package com.yupi.springbootinit.manager;


import com.google.common.util.concurrent.RateLimiter;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import org.springframework.stereotype.Service;

@Service
public class GuavaLimiterManager {
    // 每秒限流5个请求
    private final RateLimiter limiter = RateLimiter.create(1.0);
    public void doLimit() {

        if (limiter.tryAcquire()) {
            // 处理请求
            System.out.println("成功");
        } else {
            // 超过流量限制，需要做何处理
            throw new BusinessException(ErrorCode.TOO_MANY_REQUEST);
            //System.out.println("超过流量限制，需要做何处理");
        }

    }

}
